package com.android.porttallymansystem.logic.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import com.android.porttallymansystem.logic.dao.WorkDao
import com.android.porttallymansystem.logic.model.Work
import kotlin.concurrent.thread

@Database(version = 1, entities = [Work::class])
abstract class WorkDatabase : RoomDatabase() {

    abstract fun workDao(): WorkDao

    companion object {

        private var instance: WorkDatabase? = null

        fun getDatabase(context: Context): WorkDatabase {
            instance?.let {
                return it
            }
            return Room.databaseBuilder(
                context,
                WorkDatabase::class.java,
                "work_database"
            ).build().apply {
                thread {
                    val workList = mutableListOf<Work>(
                        Work(1, "工作1", "任务1", 2022,
                            1, 5, false),
                        Work(1, "工作2", "任务2", 2022,
                            1, 6, false),
                        Work(1, "工作3", "任务3", 2022,
                            1, 7, true),
                        Work(1, "工作4", "任务4", 2022,
                            1, 8, false),
                        Work(1, "工作5", "任务5", 2022,
                            1, 9, true),
                        Work(1, "工作6", "任务6", 2022,
                            1, 10, false),
                        Work(1, "工作7", "任务7", 2022,
                            1, 11, false),
                        Work(1, "工作8", "任务8", 2022,
                            1, 12, false),
                        Work(1, "工作9", "任务9", 2022,
                            1, 13, false),
                        Work(1, "工作10", "任务10", 2022,
                            1, 14, false),
                        Work(1, "工作11", "任务11", 2022,
                            1, 15, false),
                        Work(1, "工作12", "任务12", 2022,
                            1, 16, false),
                        Work(1, "工作13", "任务13", 2022,
                            1, 17, false),
                        Work(1, "工作14", "任务14", 2022,
                            1, 18, false),
                        Work(1, "工作15", "任务15", 2022,
                            1, 19, false),
                        Work(1, "工作16", "任务16", 2022,
                            1, 20, false),
                        Work(2, "工作1", "任务1", 2022,
                            1, 5, false),
                        Work(2, "工作2", "任务2", 2022,
                            1, 6, false),
                        Work(2, "工作3", "任务3", 2022,
                            1, 7, true),
                        Work(2, "工作4", "任务4", 2022,
                            1, 8, false),
                        Work(2, "工作5", "任务5", 2022,
                            1, 9, true),
                        Work(2, "工作6", "任务6", 2022,
                            1, 10, false),
                        Work(2, "工作7", "任务7", 2022,
                            1, 11, false),
                        Work(2, "工作8", "任务8", 2022,
                            1, 12, false),
                        Work(2, "工作9", "任务9", 2022,
                            1, 13, false),
                        Work(2, "工作10", "任务10", 2022,
                            1, 14, false),
                        Work(2, "工作11", "任务11", 2022,
                            1, 15, false),
                        Work(2, "工作12", "任务12", 2022,
                            1, 16, false),
                        Work(2, "工作13", "任务13", 2022,
                            1, 17, false),
                        Work(2, "工作14", "任务14", 2022,
                            1, 18, false),
                        Work(2, "工作15", "任务15", 2022,
                            1, 19, false),
                        Work(2, "工作16", "任务16", 2022,
                            1, 20, false),
                        Work(3, "工作1", "任务1", 2022,
                            1, 5, false),
                        Work(3, "工作2", "任务2", 2022,
                            1, 6, false),
                        Work(3, "工作3", "任务3", 2022,
                            1, 7, true),
                        Work(3, "工作4", "任务4", 2022,
                            1, 8, false),
                        Work(3, "工作5", "任务5", 2022,
                            1, 9, true),
                        Work(3, "工作6", "任务6", 2022,
                            1, 10, false),
                        Work(3, "工作7", "任务7", 2022,
                            1, 11, false),
                        Work(3, "工作8", "任务8", 2022,
                            1, 12, false),
                        Work(3, "工作9", "任务9", 2022,
                            1, 13, false),
                        Work(3, "工作10", "任务10", 2022,
                            1, 14, false),
                        Work(3, "工作11", "任务11", 2022,
                            1, 15, false),
                        Work(3, "工作12", "任务12", 2022,
                            1, 16, false),
                        Work(3, "工作13", "任务13", 2022,
                            1, 17, false),
                        Work(3, "工作14", "任务14", 2022,
                            1, 18, false),
                        Work(3, "工作15", "任务15", 2022,
                            1, 19, false),
                        Work(3, "工作16", "任务16", 2022,
                            1, 20, false),
                        Work(4, "工作1", "任务1", 2022,
                            1, 5, false),
                        Work(4, "工作2", "任务2", 2022,
                            1, 6, false),
                        Work(4, "工作3", "任务3", 2022,
                            1, 7, true),
                        Work(4, "工作4", "任务4", 2022,
                            1, 8, false),
                        Work(4, "工作5", "任务5", 2022,
                            1, 9, true),
                        Work(4, "工作6", "任务6", 2022,
                            1, 10, false),
                        Work(4, "工作7", "任务7", 2022,
                            1, 11, false),
                        Work(4, "工作8", "任务8", 2022,
                            1, 12, false),
                        Work(4, "工作9", "任务9", 2022,
                            1, 13, false),
                        Work(4, "工作10", "任务10", 2022,
                            1, 14, false),
                        Work(4, "工作11", "任务11", 2022,
                            1, 15, false),
                        Work(4, "工作12", "任务12", 2022,
                            1, 16, false),
                        Work(4, "工作13", "任务13", 2022,
                            1, 17, false),
                        Work(4, "工作14", "任务14", 2022,
                            1, 18, false),
                        Work(4, "工作15", "任务15", 2022,
                            1, 19, false),
                        Work(4, "工作16", "任务16", 2022,
                            1, 20, false),
                        Work(5, "工作1", "任务1", 2022,
                            1, 5, false),
                        Work(5, "工作2", "任务2", 2022,
                            1, 6, false),
                        Work(5, "工作3", "任务3", 2022,
                            1, 7, true),
                        Work(5, "工作4", "任务4", 2022,
                            1, 8, false),
                        Work(5, "工作5", "任务5", 2022,
                            1, 9, true),
                        Work(5, "工作6", "任务6", 2022,
                            1, 10, false),
                        Work(5, "工作7", "任务7", 2022,
                            1, 11, false),
                        Work(5, "工作8", "任务8", 2022,
                            1, 12, false),
                        Work(5, "工作9", "任务9", 2022,
                            1, 13, false),
                        Work(5, "工作10", "任务10", 2022,
                            1, 14, false),
                        Work(5, "工作11", "任务11", 2022,
                            1, 15, false),
                        Work(5, "工作12", "任务12", 2022,
                            1, 16, false),
                        Work(5, "工作13", "任务13", 2022,
                            1, 17, false),
                        Work(5, "工作14", "任务14", 2022,
                            1, 18, false),
                        Work(5, "工作15", "任务15", 2022,
                            1, 19, false),
                        Work(5, "工作16", "任务16", 2022,
                            1, 20, false),
                        Work(6, "工作1", "任务1", 2022,
                            1, 5, false),
                        Work(6, "工作2", "任务2", 2022,
                            1, 6, false),
                        Work(6, "工作3", "任务3", 2022,
                            1, 7, true),
                        Work(6, "工作4", "任务4", 2022,
                            1, 8, false),
                        Work(6, "工作5", "任务5", 2022,
                            1, 9, true),
                        Work(6, "工作6", "任务6", 2022,
                            1, 10, false),
                        Work(6, "工作7", "任务7", 2022,
                            1, 11, false),
                        Work(6, "工作8", "任务8", 2022,
                            1, 12, false),
                        Work(6, "工作9", "任务9", 2022,
                            1, 13, false),
                        Work(6, "工作10", "任务10", 2022,
                            1, 14, false),
                        Work(6, "工作11", "任务11", 2022,
                            1, 15, false),
                        Work(6, "工作12", "任务12", 2022,
                            1, 16, false),
                        Work(6, "工作13", "任务13", 2022,
                            1, 17, false),
                        Work(6, "工作14", "任务14", 2022,
                            1, 18, false),
                        Work(6, "工作15", "任务15", 2022,
                            1, 19, false),
                        Work(6, "工作16", "任务16", 2022,
                            1, 20, false)
                    )

                    val workDao = this.workDao()
                    if (workDao.loadWork().isEmpty()) {
                        for (work in workList) {
                            workDao.insertWork(work)
                        }
                    }
                }

                instance = this
            }
        }
    }
}